package org.grill_fish.fla.basics
{
	
	import com.adobe.serialization.json.*;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.grill_fish.fla.events.GF_DATALOADED_EVENT;
	import org.grill_fish.fla.events.GF_EXCEPT;
	
	public class GF_GetList extends EventDispatcher
	{
		private var _request:URLRequest = new URLRequest();
		private var _loader : URLLoader = new URLLoader();
		private var _url: String = "http://www.tabex.org/pages/music/sharelist.php";
		public function GF_GetList()
		{
		}
		
		private function loaded(o:Object):void{
			/*
			currentTarget	flash.net.URLLoader (@6b088e1)	
			[inherited]	
			bytesLoaded	333 [0x14d]	
			bytesTotal	333 [0x14d]	
			data	"["http:\\/\\/music.tabex.org\\/downloads\\/music\\/unknow.mp3","http:\\/\\/music.tabex.org\\/downloads\\/music\\/far_away_from_home.mp3","http:\\/\\/music.tabex.org\\/downloads\\/music\\/uness_my_time.mp3","http:\\/\\/music.tabex.org\\/downloads\\/music\\/just_good_friends.mp3","http:\\/\\/music.tabex.org\\/downloads\\/music\\/you_will_always_be_mine.mp3"]"	
			dataFormat	"text"	
			*/
			if(o.currentTarget.bytesLoaded){
				var t:Array = null;
				try{
					t = JSON.decode(o.currentTarget.data) as Array;
					this.dispatchEvent(new GF_DATALOADED_EVENT(t, GF_DATALOADED_EVENT.eventname));
				}catch(e:Error){
					this.dispatchEvent(new GF_DATALOADED_EVENT(o.currentTarget.data, GF_DATALOADED_EVENT.eventname));
				}
			}
			else{
				this.dispatchEvent(new GF_DATALOADED_EVENT(null, GF_DATALOADED_EVENT.eventname));
			}
		}
		
		private function except(o:Object):void{
			this.dispatchEvent(new GF_EXCEPT(o.message, GF_EXCEPT.eventname));
		}
		
		public function get_list(url:String = null):void{
			if(url){
				this._url = url;
			}
			this._loader.addEventListener(Event.COMPLETE, this.loaded);
			this._loader.addEventListener(IOErrorEvent.IO_ERROR, this.except);
			this._request.url = this._url;
			this._loader.load(this._request);
		}

	}
}